package com.testdocs.client.project.dashboard;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.testdocs.client.common.dialog.confirm.ConfirmDialog;
import com.testdocs.client.common.dialog.confirm.ConfirmDialog.ActionConfirmedHandler;
import com.testdocs.client.project.i18n.ProjectConstants;

public class DashboardViewImpl extends Composite implements DashboardView {
	@UiTemplate("DashboardView.ui.xml")
	interface DashboardViewUiBinder extends
			UiBinder<Widget, DashboardViewImpl> {
	}

	private static DashboardViewUiBinder uiBinder = GWT
			.create(DashboardViewUiBinder.class);

	@UiField
	Label dashboardLabel;
	@UiField
	Label projectNameLabel;
	@UiField
	Label projectName;
	
	@UiField
	Label projectRemoveLabel;
	@UiField (provided=true)
	Anchor removeButton;
	
	ConfirmDialog confirmDialog;
	
	private ProjectConstants projectConstants = (ProjectConstants) GWT.create(ProjectConstants.class);
	
	private Presenter presenter;
	
	public DashboardViewImpl() {
		removeButton = new Anchor(projectConstants.remove(), false);
		initWidget(uiBinder.createAndBindUi(this));
		
		dashboardLabel.setText(projectConstants.dashboardLabel());
		projectNameLabel.setText(projectConstants.projectNameLabel());
		projectRemoveLabel.setText(projectConstants.removeLabel());
		confirmDialog = new ConfirmDialog(projectConstants.remove(),
				projectConstants.removeConfirmation());
	}

	@Override
	public void setProjectName(String name) {
		projectName.setText(name);
	}

	@Override
	public void setPresenter(Presenter presenter) {
		this.presenter = presenter;
	}
	
	@UiHandler("removeButton")
	void handleClick(ClickEvent e) {
		confirmDialog.show(new ActionConfirmedHandler() {
			@Override
			public void handle() {
				presenter.onRemoveProject();
			}
		});
	}
}
